package com.xyh.controller;

import com.xyh.entity.*;
import com.xyh.service.*;
import com.xyh.utils.Util;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class YxcMainController {

    @Resource
    private YxcUserService yxcUserService;

    @Resource
    private YxcCarService yxcCarService;

    @Resource
    private YxcAmountService yxcAmountService;

    @Resource
    private YxcUsersYhqService yxcUsersYhqService;

    @Resource
    private YxcYhqService yxcYhqService;

    @RequestMapping("to_yxc_main")
    public String toYxcMain(HttpServletRequest request, Model model){
        String login_telephone = request.getParameter("login_telephone");
        int situation = Integer.parseInt(request.getParameter("situation"));
        YxcUser yxcUser = yxcUserService.selectByTelephone(login_telephone);
        List<YxcCar> carList = null;  // situation 为1时需要
        YxcCar yxcCar = null;
        if (situation == 0){    //车辆入场
            int current_page = Integer.parseInt(request.getParameter("current_page"));
            int page_size = Integer.parseInt(request.getParameter("page_size"));

            yxcCar = yxcCarService.selectOneCarByphoneAndPage(login_telephone,current_page,page_size);
            int showWhat = 0;   //默认存在但未入场
            if (yxcCar != null && yxcCar.getIs_init() == 1) showWhat = 1;   //存在且已经入场
            else if (yxcCar == null) showWhat = 2;   //不存在
            int count = yxcCarService.getInitCarCount(login_telephone);
            YxcAmount yxcAmount = yxcAmountService.getAmountByPhone(login_telephone);

            model.addAttribute("showWhat",showWhat);
            model.addAttribute("car_rc",yxcCar);
            model.addAttribute("current_page",current_page);
            model.addAttribute("page_size",page_size);
            model.addAttribute("isShowNextNum",yxcUser.getVip_state() == 0 ? 3:5);
            model.addAttribute("ts",yxcUser.getVip_state() == 0 ? "您是普卡会员,最多可绑定3张车牌":"您是金卡会员,最多可绑定5张车牌");
            model.addAttribute("is_disabled",count == 1);
            model.addAttribute("yxcAmount",yxcAmount);
        } else if (situation == 1) {    //车辆管理
            carList = yxcCarService.selectAllCarByphone(login_telephone);
            Util.fillToCarList(carList,yxcUser.getVip_state() == 0 ? 3:5);
            model.addAttribute("carList",carList);   // situation = 0 / 1
        } else if (situation == 3) {    //缴费记录
            int current_page = Integer.parseInt(request.getParameter("current_page"));
            int page_size = Integer.parseInt(request.getParameter("page_size"));

            List<YxcAmount> amountList = yxcAmountService.getAountByPhoneAndPage(login_telephone,current_page,page_size);
            List<YxcAmount> amountListNext = yxcAmountService.getAountByPhoneAndPage(login_telephone, current_page + 1, page_size);
            model.addAttribute("amountList",amountList);
            model.addAttribute("current_page",current_page);
            model.addAttribute("page_size",page_size);
            model.addAttribute("isShowNext",amountListNext.size() > 0);
        } else if (situation == 4){   //待人缴费
            int isShowDontNeedJf = Integer.parseInt(request.getParameter("isShowDontNeedJf"));
            model.addAttribute("isShowDontNeedJf",isShowDontNeedJf);
        } else if (situation == 5){    //积分商城
            int showWhat = Integer.parseInt(request.getParameter("showWhat"));
            List<YxcYhq> yhqList = yxcYhqService.getAllYhq();
            model.addAttribute("yhqList",yhqList);
            model.addAttribute("showWhat",showWhat);
        }
        model.addAttribute("situation",situation);
        model.addAttribute("yxcUser",yxcUser);
        model.addAttribute("show_login_telephone",login_telephone.substring(0,3) + "******" + login_telephone.substring(9));
        model.addAttribute("login_telephone",login_telephone);
        return "yxc_main";
    }

    @RequestMapping("/to_yxc_amount_jf")
    public String toYxcAmountJf(HttpServletRequest request,Model model){
        int amount_id = Integer.parseInt(request.getParameter("amount_id"));
        int vip_state = Integer.parseInt(request.getParameter("vip_state"));
        int car_id = Integer.parseInt(request.getParameter("car_id"));
        String login_telephone = request.getParameter("login_telephone");
        System.out.println("toYxcAmountJf:" + login_telephone);

        YxcAmount yxcAmount = yxcAmountService.getAmountById(amount_id);
        String endTime = Util.getTimeNow();
        int total_time = Util.getDifferHour(yxcAmount.getStart_time(), endTime);   //总时长
        int pay_number = Util.getNeedPayNumber(total_time,vip_state);
        List<YxcUsersYhq> yhqList = yxcUsersYhqService.getAllYhqByPhone(yxcAmount.getOwner_telephone());

        model.addAttribute("login_telephone",login_telephone);
        model.addAttribute("car_id",car_id);
        model.addAttribute("amount_id",amount_id);
        model.addAttribute("car_number",yxcAmount.getCar_number());
        model.addAttribute("owner_telephone",yxcAmount.getOwner_telephone());
        model.addAttribute("start_time",yxcAmount.getStart_time());
        model.addAttribute("end_time",endTime);
        model.addAttribute("total_time",total_time);
        model.addAttribute("pay_number",pay_number);
        model.addAttribute("yhqList",yhqList);
        model.addAttribute("vip_state",vip_state);
        return "yxc_amount_jf";
    }

    @RequestMapping("/check_amount")
    public String checkAmount(HttpServletRequest request, RedirectAttributes attributes){
        String car_number = request.getParameter("car_number");
        String login_telephone = request.getParameter("login_telephone");
        int vip_state = Integer.parseInt(request.getParameter("vip_state"));
        YxcAmount yxcAmount = yxcAmountService.getAmountByCarNumber(car_number);
        if (yxcAmount == null) {
            attributes.addAttribute("login_telephone", login_telephone);
            attributes.addAttribute("situation", 4);
            attributes.addAttribute("isShowDontNeedJf",1);   //显示无需缴费的提示
            return "redirect:to_yxc_main";
        }
        Integer car_id = yxcCarService.getCarIdByCarNumber(car_number);
        attributes.addAttribute("login_telephone",login_telephone);
        attributes.addAttribute("amount_id",yxcAmount.getAmount_id());
        attributes.addAttribute("vip_state",vip_state);
        attributes.addAttribute("car_id",car_id);
        return "redirect:to_yxc_amount_jf";
    }

    @RequestMapping("/check_jfsc")
    public String checkJfsc(HttpServletRequest request,RedirectAttributes attributes,YxcUser yxcUser){
        int yhq1 = Integer.parseInt(request.getParameter("yhq1"));
        int yhq2 = Integer.parseInt(request.getParameter("yhq2"));
        int yhq3 = Integer.parseInt(request.getParameter("yhq3"));
        String user_name = request.getParameter("user_name");
        int[] yhqNumArr = {yhq1,yhq2,yhq3};

        List<YxcYhq> yhqList = yxcYhqService.getAllYhq();
        int numberOfBuy = Util.getNumberOfBuy(yhqList,yhqNumArr);
        if (numberOfBuy > yxcUser.getJifen()) {    //积分不足以购买
            attributes.addAttribute("login_telephone",yxcUser.getTelephone());
            attributes.addAttribute("situation",5);
            attributes.addAttribute("showWhat",1);  //1 表示积分不足，购买失败
            return "redirect:to_yxc_main";
        }
        yxcUserService.updateJifenAndYhqByPhone(yxcUser.getTelephone(),numberOfBuy,(yhq1 + yhq2 + yhq3));  //修改 user表 的积分和优惠券数量
        for (int i = 0; i < yhqNumArr.length; i++) {
            int flag = yxcUsersYhqService.updateCyNumByPhoneAndBh(yxcUser.getTelephone(),yhqList.get(i).getYhq_bh(),yhqNumArr[i]);
            if (flag == 0 && yhqNumArr[i] != 0) {    //修改失败，说明该用户的拥有的该编号的数量为0，且本次购买数量不为0，需要做插入操作
                yxcUsersYhqService.addOneUserYhq(user_name,yxcUser.getTelephone(),yhqList.get(i).getYhq_bh(),yhqList.get(i).getYhq_name(),yhqNumArr[i]);
            }
        }
        attributes.addAttribute("login_telephone",yxcUser.getTelephone());
        attributes.addAttribute("situation",5);
        attributes.addAttribute("showWhat",2);  //2 购买成功
        return "redirect:to_yxc_main";
    }
}
